package com.gree.framework.manager.factory;

import com.gree.common.entity.SysLogininfor;
import com.gree.common.entity.SysOperLog;
import com.gree.common.service.ISysOperLogService;
import com.gree.common.service.impl.SysLogininforServiceImpl;
import com.gree.common.utils.*;
import com.gree.common.utils.spring.SpringxUtils;
import com.gree.framework.service.impl.GscmUserServiceImpl;
import eu.bitwalker.useragentutils.UserAgent;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.context.request.RequestAttributes;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.TimerTask;

public class AsyncFactory
{
    private static final Logger sys_user_logger = LoggerFactory.getLogger("sys-user");



    /**
     * 操作日志记录
     *
     * @param operLog 操作日志信息
     * @return 任务task
     */
    public static TimerTask recordOper(final SysOperLog operLog)
    {
        return new TimerTask()
        {
            @Override
            public void run()
            {
                // 远程查询操作地点
                operLog.setOper_location("");
                SpringxUtils.getBean(ISysOperLogService.class).insertOperlog(operLog);
            }
        };
    }

    /**
     * 记录登录信息
     *
     * @param username 用户名
     * @param status 状态
     * @param message 消息
     * @param args 列表
     * @return 任务task
     */
    public static TimerTask recordLogininfor(final String username,final String comp, final String status, final String message, final Object... args)
    {
        final UserAgent userAgent = UserAgent.parseUserAgentString(ServletUtils.getRequest().getHeader("User-Agent"));
        RequestAttributes requestAttributes = RequestContextHolder.getRequestAttributes();
        HttpServletRequest request = ((ServletRequestAttributes)requestAttributes).getRequest();
        final String ip = IpUtils.getIpAddr(request);
        return new TimerTask()
        {
            @Override
            public void run()
            {
                StringBuilder s = new StringBuilder();
                s.append(LogUtils.getBlock(ip));
                s.append(comp);
                s.append(LogUtils.getBlock(username));
                s.append(LogUtils.getBlock(status));
                s.append(LogUtils.getBlock(message));
                // 打印信息到日志
                sys_user_logger.info(s.toString(), args);
                // 获取客户端操作系统
                String os = userAgent.getOperatingSystem().getName();
                // 获取客户端浏览器
                String browser = userAgent.getBrowser().getName();
                // 封装对象
                SysLogininfor logininfor = new SysLogininfor();
                logininfor.setLogin_name(username);
                logininfor.setComp(comp);
                logininfor.setIpaddr(ip);
                logininfor.setLogin_location(" ");
                logininfor.setBrowser(browser);
                logininfor.setOs(os);
                logininfor.setMsg(message);
                logininfor.setLogin_time(new Date());
                // 日志状态
                if (StringUtils.equalsAny(status, Constants.LOGIN_SUCCESS, Constants.LOGOUT, Constants.REGISTER))
                {
                    logininfor.setStatus(Constants.SUCCESS);
                }
                else if (Constants.LOGIN_FAIL.equals(status))
                {
                    logininfor.setStatus(Constants.FAIL);
                }
                // 插入数据
                SpringxUtils.getBean(SysLogininforServiceImpl.class).insertLogininfor(logininfor);
                // feat(21.04.19)180686
                SpringxUtils.getBean(GscmUserServiceImpl.class).updateLxdt(username,comp);
            }
        };
    }

    public static TimerTask recordMenu(final String username,final String comp, final String teno)
    {
        return new TimerTask()
        {
            @Override
            public void run()
            {
                SpringxUtils.getBean(ISysOperLogService.class).insertMenulog(username,comp,teno,new Date());
            }
        };
    }
}

